package com.glacier.yuyuan.gen.webapp.service.impl;

import cn.hutool.db.meta.MetaUtil;
import com.glacier.common.support.service.impl.BaseServiceImpl;
import com.glacier.yuyuan.gen.webapp.domain.GenDatasource;
import com.glacier.yuyuan.gen.webapp.mapper.GenDatasourceMapper;
import com.glacier.yuyuan.gen.webapp.service.DataSourceService;
import com.glacier.yuyuan.gen.webapp.service.GenDatasourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * date 2023-01-28 21:59
 * 数据源管理 业务层
 *
 * @author glacier
 * @version 1.0.0
 */
@Transactional(readOnly = true)
@Service
public class GenDatasourceServiceImpl extends BaseServiceImpl<GenDatasourceMapper, GenDatasource>
        implements GenDatasourceService {
    private DataSourceService dataSourceService;

    public GenDatasourceServiceImpl() {
        super(GenDatasourceMapper.class);
    }

    @Autowired
    public void setDataSourceService(DataSourceService dataSourceService) {
        this.dataSourceService = dataSourceService;
    }

    /**
     * 根据数据源 探查所有表
     *
     * @param sourceId
     * @return
     */
    @Override
    public List<String> getTables(String sourceId) {
        if (sourceId == null || sourceId.isBlank()) {
            throw new IllegalArgumentException("参数sourceId不能为空！");
        }
        return MetaUtil.getTables(dataSourceService.getDataSource(sourceId));
    }
}
